package com.fuqiang.nightshopadmin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fuqiang.nightshopadmin.model.vo.LoginVO;
import com.fuqiang.nightshopadmin.model.vo.UmsAdminVO;
import com.fuqiang.nightshopadmin.service.IUmsAdminService;
import com.fuqiang.nightshopadmin.service.IUmsRoleService;
import com.fuqiang.nightshopcommon.api.R;
import com.fuqiang.nightshopumsmodel.entity.UmsAdmin;
import com.fuqiang.nightshopumsmodel.entity.UmsRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author fuqiang
 * @since 2020-04-14
 */
@RestController
@RequestMapping("/admin/")
@Api(tags = "用户模块")
public class UmsAdminController {

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private IUmsAdminService umsAdminService;
    @Autowired
    private IUmsRoleService umsRoleService;


    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public R<Map<String,Object>> getAdminInfo(Principal principal) {
        if(principal==null){
            return R.unauthorized(null);
        }
        String username = principal.getName();
        UmsAdmin umsAdmin = umsAdminService.getAdminByUsername(username);
        Map<String, Object> data = new HashMap<>();
        data.put("username", umsAdmin.getUsername());
        data.put("roles", new String[]{"TEST"});
        data.put("menus", umsRoleService.getMenuList(umsAdmin.getId()));
        data.put("icon", umsAdmin.getIcon());
        return R.success(data);
    }

    // 用户登录
    @ApiOperation("登录")
    @PostMapping("login")
    public R<Map<String,String>> login(@Valid @RequestBody LoginVO loginVO){
        String token = umsAdminService.login(loginVO.getUsername(),loginVO.getPassword());
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return R.success(tokenMap);
    }

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public R<UmsAdmin> register(@RequestBody UmsAdminVO umsAdminVO, BindingResult result) {
        UmsAdmin umsAdmin = umsAdminService.register(umsAdminVO);
        if (umsAdmin == null) {
            R.failed();
        }
        return R.success(umsAdmin);
    }

    @ApiOperation("根据用户名或姓名分页获取用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public R<IPage<UmsAdmin>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<UmsAdmin> adminList = umsAdminService.list(keyword, pageSize, pageNum);
        return R.success(adminList);
    }

    @ApiOperation("获取指定用户的角色")
    @RequestMapping(value = "/role/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public R<List<UmsRole>> getRoleList(@PathVariable Long adminId) {
        List<UmsRole> roleList = umsAdminService.getRoleList(adminId);
        return R.success(roleList);
    }

    @ApiOperation("给用户分配角色")
    @RequestMapping(value = "/role/update", method = RequestMethod.POST)
    @ResponseBody
    public R updateRole(@RequestParam(name = "adminId") Long adminId,
                                   @RequestParam("roleIds") List<Long> roleIds) {
        int count = umsAdminService.updateRole(adminId, roleIds);
        if (count >= 0) {
            return R.success(count);
        }
        return R.failed();
    }

    @ApiOperation("修改指定用户信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public R update(@PathVariable Long id, @RequestBody UmsAdminVO admin) {
        int count = umsAdminService.update(id, admin);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed();
    }

}

